//
// Created by Zhang on 2023/5/5.
//

#include <iostream>
#include <memory>
#include <string>
#include "gRpcHelper.h"

#ifdef BAZEL_BUILD
#include "examples/protos/myhelloworld.grpc.pb.h"
#else
#include "../protos/myhelloworld.grpc.pb.h"
#endif


#include <grpcpp/channel.h>
#include <grpcpp/grpcpp.h>

#ifdef BAZEL_BUILD
#include "examples/protos/helloworld.grpc.pb.h"
#include "helloworld.grpc.pb.h"
#endif

using grpc::Channel;
using grpc::ClientContext;
using grpc::Status;

// grpc客户端2
using helloworld::Greeter;   // 服务
using helloworld::HelloRequest;   // 请求
using helloworld::HelloReply;  // 回复
using helloworld::OperateType;  // 枚举

class GreeterClient2 {
public:

    GreeterClient2(std::shared_ptr<Channel> channel):stub_(Greeter::NewStub(channel)) {}

    int say_hello() {
        HelloRequest request; // 请求
        HelloReply reply;  // 回复
        ClientContext context;

        request.set_num1(8.14);
        request.set_num2(2.264);
        request.set_optype(OperateType::Multiplication);

        Status status = stub_->SayHello(&context, request, &reply);

        if (status.ok()) {
            return reply.result();
        } else {
            std::cout << "ERROR: " << status.error_code() << ": " << status.error_message() << std::endl;
            return 0;
        }
    }

private:
    std::unique_ptr<Greeter::Stub> stub_;
};

int grpcTest() {
    GreeterClient2 greeter(grpc::CreateChannel("192.168.51.201:7001", grpc::InsecureChannelCredentials()));
    int reply = greeter.say_hello();
    std::cout << "Greeter received: " << reply << std::endl;
    return 0;
}

#ifndef _MY_GRPC_TEST
//int main() {
//    printf("Hello, World!\n");
//    grpcTest();
//    return 0;
//}
#endif

